package com.example.template.service.impl;

import com.example.template.common.base.impl.BaseServiceImpl;
import com.example.template.dto.UserDTO;
import com.example.template.entity.User;
import com.example.template.mapper.UserMapper;
import com.example.template.service.UserService;
import com.example.template.vo.UserEditModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @className: UserServiceImpl
 * @description: 用户服务实现类
 * @author: Kuangkeli
 * @date: 2024/01/18 22:36
 **/
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDTO login(String username, String password) {
        User user = userMapper.findUserByUsernameAndPassword(username, password);
        if (user != null) {
            UserDTO userDTO = new UserDTO();
            BeanUtils.copyProperties(user, userDTO);
            return userDTO;
        }
        return null;
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public String getRolesByUserID(Long UserID) {
        return userMapper.getRolesByUserId(UserID);
    }

    @Override
    public int updateUser(UserEditModel userEditModel) {
        userMapper.updateUserEdit(userEditModel);
        userMapper.updateUserRole(userEditModel.getUserId(),Long.parseLong(userEditModel.getRoles()));
        return 0;
    }

}
